<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="联络站名称" prop="csName">
        <el-input
          v-model="queryParams.csName"
          placeholder="请输入联络站名称"
          clearable
          style="cursor: pointer"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属区域" prop="area">
        <el-select
          v-model="queryParams.area"
          placeholder="请选择活动类型"
          style="width: 100%"
        >
          <el-option
            v-for="dict in dict.type.sys_area_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['dbxx:lianLuo:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['dbxx:lianLuo:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['jylz:jylzActivity:remove']"
          >删除</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:activity:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="lianLuoList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="id" align="center" prop="id" v-if="true" /> -->
      <el-table-column label="联络站名称" align="center" prop="csName" />
      <el-table-column label="负责人" align="center" prop="leader" />
      <el-table-column label="电话" align="center" prop="phone" />
      <el-table-column label="所属区域" align="center" prop="area">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_area_type"
            :value="scope.row.area"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dbxx:lianLuo:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dbxx:lianLuo:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改履职活动对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="联络站名称" prop="csName">
              <el-input v-model="form.csName" placeholder="请输入联络站名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="负责人" prop="leader">
              <el-input v-model="form.leader" placeholder="请输入负责人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="电话" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入电话" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属区域" prop="">
              <el-select
                v-model="form.area"
                placeholder="请选择所属区域"
                style="width: 100%"
                @change="setSearchJingwei"
              >
                <el-option
                  v-for="dict in dict.type.sys_area_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联络站地址" prop="addr">
              <el-input
                v-model="form.addr"
                disabled
                placeholder="请输入联络站地址"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <baidu-map
          style="display: flex; flex-direction: column-reverse"
          id="allmap"
          @ready="mapReady"
          @click="getLocation"
          :scroll-wheel-zoom="true"
        >
          <div
            style="
              display: flex;
              justify-content: center;
              position: absolute;
              right: 0;
              top: 0;
            "
          >
            <bm-auto-complete
              v-model="searchJingwei"
              :sugStyle="{ zIndex: 999999 }"
            >
              <el-input
                v-model="searchJingwei"
                style="width: 300px; margin-right: 15px"
                placeholder="输入地址"
              ></el-input>
            </bm-auto-complete>
            <el-button type="primary" @click="getBaiduMapPoint">搜索</el-button>
          </div>
          <bm-map-type
            :map-types="['BMAP_NORMAL_MAP', 'BMAP_HYBRID_MAP']"
            anchor="BMAP_ANCHOR_TOP_LEFT"
          ></bm-map-type>
          <bm-marker
            v-if="infoWindowShow"
            :position="{ lng: longitude, lat: latitude }"
          >
            <bm-label
              content=""
              :labelStyle="{ color: 'red', fontSize: '24px' }"
              :offset="{ width: -35, height: 30 }"
            />
          </bm-marker>
          <bm-info-window
            :position="{ lng: longitude, lat: latitude }"
            :show="infoWindowShow"
            @clickclose="infoWindowClose"
          >
            <p>纬度:{{ this.latitude }}</p>
            <p>经度:{{ this.longitude }}</p>
          </bm-info-window>
        </baidu-map>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addLianLuoList,
  getLianLuoItem,
  deleteLianLuo,
  addLianLuo,
  updateLianLuoList,
} from "@/api/dbxx/lianLuo/index";
import Vue from "vue";
import { listData } from "@/api/system/dict/data";
// 全局注册
// 全局注册将一次性引入百度地图组件库的所有组件。
import BaiduMap, { BmLocalSearch } from "vue-baidu-map";
Vue.use(BaiduMap, {
  // ak 是在百度地图开发者平台申请的密钥 详见 http://lbsyun.baidu.com/apiconsole/key */
  ak: "QX5LMaojoADuhhzwkGmSSZUBHrUwG7CG", //个人手机号私人使用-July_zhengfj  oukWFWRKCGktlhX25uFBpEt0nV4iayz1
});
Vue.use(BmLocalSearch);
export default {
  name: "Activity",
  dicts: ["sys_area_type"],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      names: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 履职活动表格数据
      lianLuoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        csName: undefined,
        area: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        area: [
          {
            required: true,
            message: "所属区域不能为空",
            trigger: "change",
          },
        ],
        csName: [
          { required: true, message: "联络站名称不能为空", trigger: "blur" },
        ],
        leader: [
          { required: true, message: "负责人不能为空", trigger: "blur" },
        ],

        phone: [{ required: true, message: "电话不能为空", trigger: "blur" }],
        addr: [
          { required: true, message: "联络站地址不能为空", trigger: "blur" },
        ],
      },
      keyword: "志丹",
      searchJingwei: "",
      infoWindowShow: false,
      longitude: "",
      latitude: "",
      point: "",
      options: [],
    };
  },
  created() {
    this.getList();
  },

  methods: {
    mapReady({ BMap, map }) {
      // 选择一个经纬度作为中心点
      this.point = new BMap.Point(108.768432, 36.822194);
      map.centerAndZoom(this.point, 12);
      this.BMap = BMap;
      this.map = map;
    },
    //点击获取经纬度
    getLocation(e) {
      this.longitude = e.point.lng;
      this.latitude = e.point.lat;
      this.form.addr = e.point.lng + "," + e.point.lat;
      this.infoWindowShow = true;
    },
    getBaiduMapPoint() {
      let that = this;
      let myGeo = new this.BMap.Geocoder();
      myGeo.getPoint(this.searchJingwei, function (point) {
        if (point) {
          that.map.centerAndZoom(point, 15);
          that.latitude = point.lat;
          that.longitude = point.lng;
          this.form.addr = e.point.lng + "," + e.point.lat;
          that.infoWindowShow = true;
        }
      });
    },
    //信息窗口关闭
    infoWindowClose() {
      this.latitude = "";
      this.longitude = "";
      this.infoWindowShow = false;
    },
    setSearchJingwei(e) {
      this.searchJingwei =
        "延安市志丹县" + this.dict.type.sys_area_type[e - 1].label;
      this.getBaiduMapPoint();
    },
    getActivityUrl(value) {
      this.form.annex = value;
    },
    /** 查询履职活动列表 */
    getList() {
      this.loading = true;
      addLianLuoList(this.queryParams).then((response) => {
        this.lianLuoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        area: undefined,
        csName: undefined,
        leader: undefined,
        phone: undefined,
        addr: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.csId);
      this.names = selection.map((item) => item.dbUser.csName);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加联络站";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.csId || this.ids;
      getLianLuoItem(id).then((response) => {
        this.loading = false;
        this.form = response.data;
        this.form.ruleId = this.form.ruleId;
        this.open = true;
        this.title = "修改联络站";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.csId != null) {
            updateLianLuoList(this.form)
              .then((response) => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          } else {
            addLianLuo(this.form)
              .then((response) => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .finally(() => {
                this.buttonLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.csId || this.ids;
      const name = row?.csName || this.names;
      this.$modal
        .confirm('是否确认删除代表名称为"' + name + '"的数据项？')
        .then(() => {
          this.loading = true;
          return deleteLianLuo(ids);
        })
        .then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {})
        .finally(() => {
          this.loading = false;
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "/system/activity/export",
        {
          ...this.queryParams,
        },
        `履职活动_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style lang="scss" scoped>
#allmap {
  width: 100%;
  height: 500px;
  position: relative;
}
::v-deep .el-input.is-disabled .el-input__inner {
  color: #000;
}
</style>
